<?php 

class Upload{
      protected  $fileName;//文件名
      protected  $fileInfo; //FILES
      protected  $fileErrorMsg;//错误信息
      protected  $fileMine;//文件上传类型
      protected  $fileExt;//文件上传格式
      protected  $ext = array();//文件扩展名
      protected  $fileMax;//文件上传大小
      protected  $filePath;//文件上传路径
      protected  $smallFilePath;//缩略图文件保存路径
      protected  $path = array();
 

	public function __construct($fileName = "file",$fileMax = 2147483648,$filePath = "./Uploads",$fileMine = array('image/jpeg','image/png','image/gif','image/jpg'),$fileExt = array('jpg','png','gif','jpeg')){


		$this->fileName = $fileName;
		$this->fileMax = $fileMax;
		$this->filePath = $filePath;
		$this->fileMine = $fileMine;
		$this->fileInfo = $_FILES[$fileName];
		$this->fileExt = $fileExt;

	}

	public function setFilePath($filePath){
		$this->filePath = $filePath;
	}
	public function setSmallFilePath($smallFilePath){
		$this->smallFilePath = $smallFilePath;
	}


	

	/*
	错误判断
	 */
	private function UpError(){
		foreach ($this->fileInfo['error'] as $key => $value) {
			if($value > 0){
				switch ($value) {
					case 1:
	                    $this->fileErrorMsg="上传文件大小超过服务器允许上传的最大值，php.ini中设置upload_max_filesize选项限制的值 ";
	                        break;
	                    case 2:
	                        $this->fileErrorMsg="上传文件大小超过HTML表单中隐藏域MAX_FILE_SIZE选项指定的值";
	                        break;
	                    case 3:
	                        $this->fileErrorMsg="文件部分被上传";
	                        break;
	                    case 4:
	                        $this->fileErrorMsg="没有选择上传文件";
	                        break;
	                    case 5:
	                        $this->fileErrorMsg="未找到临时目录";
	                        break;
	                    case 6:
	                        $this->fileErrorMsg="文件写入失败";
	                        break;
	                    case 7:
	                        $this->fileErrorMsg="php文件上传扩展没有打开 ";
	                        break;
	                    case 8:
	                        $this->fileErrorMsg="";
	                        break;
				}
				return false;
			}
				
		}
		return true;
	}

	//检测文件类型
    private function UpMine(){
    	
    	foreach ($this->fileInfo['type'] as $key => $value) {
    		if(!in_array(strtolower($value),$this->fileMine)) {
	            $this->fileErrorMsg = "文件上传类型不对";
	            return false;
	        }
    	}
        
        return true;
 
    }

    //检测文件格式
    private function UpExt(){
    	foreach ($this->fileInfo['name'] as $key => $value) {
    		$ext = pathinfo($value,PATHINFO_EXTENSION);

    		$this->ext[] = $ext;
    		if(!in_array(strtolower($ext),$this->fileExt)){
	            $this->fileErrorMsg="文件格式不对";
	            return false;
	        }

    	}
      
       return true;
    }

    //检测文件大小
    private function UpSize(){
    	foreach ($this->fileInfo['size'] as $key => $value) {
    		if($value > $this->fileMax){
	            $this->fileErrorMsg = "文件过大";
	            return false;
	        }
    	}
      
        
        return true;
    }

     //检测文件是否HTTP
    private function UpPost(){
    	foreach ($this->fileInfo['tmp_name'] as $key => $value) {
    		if(!is_uploaded_file($value)){
	            $this->fileErrorMsg="恶意上传";
	            return false;
	        }else{
	        	return true;
	        }
    	}
        
        return true;
    }

     //检测文件路径
    private function UpPath(){
        if(!file_exists($this->filePath)){
            mkdir($this->filePath,0777,true);
        }
    }
    private function UpSmallPath(){
        if(!file_exists($this->smallFilePath)){
            mkdir($this->smallFilePath,0777,true);
        }
    }
     //文件名防止重复
    private function Upname(){
        return md5(uniqid(microtime(true),true));
    }

    
    private function img_create_small($big_img, $width, $height, $small_img,$quality) {//原始大图地址，缩略图宽度，高度，缩略图地址,压缩率
		$imgage = getimagesize($big_img); //得到原始大图片
		$im;
		switch ($imgage[2]) { // 图像类型判断
			case 1:
				$im = imagecreatefromgif($big_img);
			break;
			case 2:
				$im = imagecreatefromjpeg($big_img);
			break;
			case 3:
				$im = imagecreatefrompng($big_img);
			break;
		}
		$src_W = $imgage[0]; //获取大图片宽度
		$src_H = $imgage[1]; //获取大图片高度

		 $src_x = 0;
        $src_y = 0;
        if($src_W/$width > $src_H/$height){
            $zh = $height;
            $zw = $zh*($src_W/$src_H);
            $src_x = ($zw-$width)/2;
        }else{
            $zw = $width;
            $zh = $zw*($src_H/$src_W);
            $src_y = ($zh-$height)/2;
        }

        $zimg = imagecreatetruecolor($zw, $zh);
        // 先把图像放满区域
        imagecopyresampled($zimg, $im, 0,0, 0,0, $zw,$zh, $src_W,$src_H);
        // 再截取到指定的宽高度
        $timg = imagecreatetruecolor($width, $height);
        imagecopyresampled($timg, $zimg, 0,0, 0+$src_x,0+$src_y, $width,$height, $zw-$src_x*2,$zh-$src_y*2);

		imagejpeg($timg, $small_img,$quality);//输出图像
	}

	//获取错误消息
    public function getErrMsg(){
    	return $this->fileErrorMsg;
    }

	//获取文件
    public function getPath(){
    	return $this->path;
    }

    //获取缩略图 
    public function getSmallimg($w = 300,$h = 300,$quality = 50){
    	$smallPath = array();
    	$this->UpSmallPath();
    	foreach ($this->path as $key => $value) {
    		$smallName = $this->Upname().'.'.$this->ext[$key];
    		$path = ($this->smallFilePath?$this->smallFilePath:$this->filePath).'/'. $smallName;
    		$smallPath[] = $smallName;
    		$this->img_create_small($this->filePath.'/'.$value,$w,$h,$path,$quality);
    	}
    	return $smallPath;
	    	
    }
    
	public function upload(){
		
		if($this->UpError() && $this->UpMine() && $this->UpExt() && $this->UpSize() && $this->UpPost()){
			$this->UpPath();

			for ($i=0,$len=count($this->fileInfo['tmp_name']);$i<$len;$i++) {
				$names = $this->Upname().'.'.$this->ext[$i];

				$path = $this->filePath.'/'. $names;
				$this->path[] = $names;
			
				if(!move_uploaded_file($this->fileInfo['tmp_name'][$i], $path)){
					return false;
				}
			}

			return true;
		}else{
			return false;
		}
		
	}

}
//接收字段名
//图片大小单位b
//上传路径
//可上传文件类型
//可上传文件后缀
//getPath 获取原图路径数组
//getSmallimg 获取缩略图片路径数据
// $upload = new Upload();
// $upload->setFilePath('./Uploads/src/'.date('Y-m-d'));
// $upload->setSmallFilePath('./Uploads/sub/'.date('Y-m-d'));
// $re = $upload->upload();
// if($re){
// 	echo json_encode(array('path'=>$upload->getPath(),'smallPath'=>$upload->getSmallimg()));
// }

	


	

	
	





 ?>